from rest_framework.generics import ListCreateAPIView

from apps.meiduo_admin.serializers.users import UserGetInfoSerializer, UserPostInfoSerializer
from apps.meiduo_admin.utils import UserDefinePagination
from apps.users.models import User
from rest_framework.permissions import *


class UserListCreateAPIView(ListCreateAPIView):
    permission_classes = [IsAuthenticated]

    # 查询用户与增加用户 由于字段与条件不同 则查询集 与 序化器均不相同
    def get_queryset(self):
        # 获取查询参数
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return User.objects.filter(username__contains=keyword)
        return User.objects.all()

    # 序化器的不同
    def get_serializer_class(self):
        # 根据请求方式来返回不同的序化器
        if self.request.method == "GET":
            return UserGetInfoSerializer
        elif self.request.method == "POST":
            return UserPostInfoSerializer

    # 分页器的实现
    pagination_class = UserDefinePagination

